{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### image 读取显示操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-03-06T08:45:11.136736Z",
     "start_time": "2019-03-06T08:45:11.132735Z"
    }
   },
   "outputs": [],
   "source": [
    "#!/usr/bin/python\n",
    "# -*- coding: utf-8 -*-"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-03-06T08:45:12.458988Z",
     "start_time": "2019-03-06T08:45:12.045230Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import cv2\n",
    "from matplotlib import pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-03-06T08:45:37.347683Z",
     "start_time": "2019-03-06T08:45:33.434915Z"
    }
   },
   "outputs": [],
   "source": [
    "img = cv2.imread(\"./sample_img/img.jpg\", -1) # 1: 显示原始图， 0：灰度图 -1：加载图像包括 alpha 通道\n",
    "cv2.imshow(\"image\", img) # 第一个参数为显示窗口的名称，第二个参数是要显示的图像\n",
    "k = cv2.waitKey(0) # cv2.waitKey()  键盘绑定函数，等待键盘按下，以毫秒为计数单位，也可以设定特定按键点击。\n",
    "if k == 27:         # wait for ESC key to exit\n",
    "    cv2.destroyAllWindows() # 销毁所有窗口，也可以用 cv2.destroyWindow() 销毁特定的窗口。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-03-06T08:45:40.725720Z",
     "start_time": "2019-03-06T08:45:39.446460Z"
    }
   },
   "outputs": [],
   "source": [
    "cv2.namedWindow(\"image_new\", cv2.WINDOW_NORMAL) # 创建一个新的窗口，这个窗口可以进行调节的\n",
    "cv2.imshow(\"image_new\", img)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyWindow(\"image_new\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "形态变换：\n",
    "\n",
    "形态变换是基于图像形状的一些简单操作。它通常在二进制图像上执行。它需要两个输入，一个是原始图像，另一个是结构元素或内核，决定操作的性质。两个基本的形态算子是侵蚀和膨胀。然后，它的不同形式，如开放，关闭，梯度等也发挥了作用。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 图像腐蚀"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-01-25T15:16:26.418065Z",
     "start_time": "2019-01-25T15:16:26.411069Z"
    }
   },
   "source": [
    " OpenCV 最基本的形态学运算——腐蚀：用图像中的暗色部分“腐蚀”掉图像中的高亮部分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-03-06T08:45:51.414579Z",
     "start_time": "2019-03-06T08:45:49.798505Z"
    }
   },
   "outputs": [],
   "source": [
    "img = cv2.imread(\"./sample_img/img.jpg\", 0)\n",
    "cv2.imshow(\"init_image\", img)\n",
    "kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 1)) # 定义结构元素，指定形状为矩形，尺寸\n",
    "erode = cv2.erode(img, kernel) # 进行腐蚀操作\n",
    "cv2.imshow(\"erode_image\", erode)\n",
    "k = cv2.waitKey(0)\n",
    "if k == 27:\n",
    "    cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 图像模糊"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "通过将图像与低通滤波器内核卷积来实现图像模糊。\n",
    "\n",
    "它有助于**消除噪音**。它实际上从图像中去除了高频内容（例如：噪声，边缘），导致边缘在应用滤波器时模糊。\n",
    "\n",
    "OpenCV主要提供四种模糊技术：均值滤波、高斯滤波、中位数滤波、双边滤波"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-03-06T08:46:01.573735Z",
     "start_time": "2019-03-06T08:45:59.289049Z"
    }
   },
   "outputs": [],
   "source": [
    "img = cv2.imread(\"./sample_img/img.jpg\", 0)\n",
    "cv2.imshow(\"init_image\", img)\n",
    "blur = cv2.blur(img, (5, 5)) # 均值滤波\n",
    "cv2.imshow(\"blur_image\", blur)\n",
    "k = cv2.waitKey(0)\n",
    "if k == 27:\n",
    "    cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### canny 边缘检测\n",
    "各个阶段介绍"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 降噪\n",
    "边缘检测容易受到噪声的影响，在进行边缘检测时，先对图像用滤波器进行降噪。\n",
    "#### 求图像的强度梯度\n",
    "然后用 Sobel 核在水平和垂直方向对平滑后的图像进行滤波，得到水平方向 $(G_x)$ 和垂直方向 $(G_y)$ 的一阶导数。从这两幅图像中，我们可以发现每个像素的边缘梯度和方向如下:\n",
    "![](https://ws1.sinaimg.cn/large/acbcfa39gy1fzjtsrivjtj207a02cdfo.jpg)\n",
    "渐变方向始终垂直于边缘。它被四舍五入到表示垂直，水平和两个对角线方向的四个角度中的一个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-03-06T08:46:12.399511Z",
     "start_time": "2019-03-06T08:46:10.357687Z"
    }
   },
   "outputs": [],
   "source": [
    "img = cv2.imread(\"./sample_img/img.jpg\", 0)\n",
    "cv2.imshow(\"init_image\", img)\n",
    "blur = cv2.blur(img, (6, 6)) # 均值滤波\n",
    "edges = cv2.Canny(blur, 100, 200)\n",
    "cv2.imshow(\"edges_image\", edges)\n",
    "k = cv2.waitKey(0)\n",
    "if k == 27:\n",
    "    cv2.destroyAllWindows()"
   ]
  }
 ],
 "metadata": {
  "hide_input": false,
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {
    "height": "29px",
    "width": "189px"
   },
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "oldHeight": 122,
   "position": {
    "height": "40px",
    "left": "411px",
    "right": "20px",
    "top": "120px",
    "width": "250px"
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "varInspector_section_display": "none",
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
